<?php

use Doraemon\pockets\constInterface\CacheDbFieldsMap;
use Doraemon\service\Event\Event;
use Doraemon\service\Event\CacheRelatedEvent;
use Doraemon\tools\Log;

class CacheMethodAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('Cache操作接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
        if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        //只监听更新操作
        if ($this->messageBody['type'] == CacheDbFieldsMap::TYPE_UPDATE) {
            $fieldsMap = CacheRelatedEvent::matchFields($this->messageBody['database'], $this->messageBody['table']);
            if (!empty($fieldsMap)) {
                CacheRelatedEvent::CacheMethod($this->messageBody, $fieldsMap);
            }
        }
        $this->setSuccess();
    }
}